package com.atguigu.tingshu.album.service.impl;

import com.alibaba.fastjson.JSON;
import com.atguigu.tingshu.album.config.VodConstantProperties;
import com.atguigu.tingshu.album.service.VodService;
import com.atguigu.tingshu.common.annotation.Log;
import com.atguigu.tingshu.vo.album.TrackMediaInfoVo;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.vod.v20180717.VodClient;
import com.tencentcloudapi.vod.v20180717.models.*;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
@Slf4j
public class VodServiceImpl implements VodService {

    @Autowired
    private VodConstantProperties vodConstantProperties;

    @SneakyThrows
    @Override
    public TrackMediaInfoVo getTrackMediaInfo(String fileId) {
        //创建Credential对象,封装api接口秘钥
        Credential cred = new Credential(vodConstantProperties.getSecretId(), vodConstantProperties.getSecretKey());
        //创建VodClient对象
        VodClient client = new VodClient(cred, "ap-guangzhou");
        // 实例化一个请求对象,每个接口都会对应一个request对象
        DescribeMediaInfosRequest req = new DescribeMediaInfosRequest();
        String[] fileIds = {fileId};
        req.setFileIds(fileIds);

        //发送请求
        DescribeMediaInfosResponse describeMediaInfosResponse = client.DescribeMediaInfos(req);
        //从describeMediaInfosResponse对象中获取文件的数据
        MediaInfo mediaInfo = describeMediaInfosResponse.getMediaInfoSet()[0];
        MediaBasicInfo basicInfo = mediaInfo.getBasicInfo();
        MediaMetaData metaData = mediaInfo.getMetaData();

        TrackMediaInfoVo trackMediaInfoVo = new TrackMediaInfoVo();

        trackMediaInfoVo.setSize(metaData.getSize());
        trackMediaInfoVo.setDuration(metaData.getDuration());
        trackMediaInfoVo.setMediaUrl(basicInfo.getMediaUrl());
        trackMediaInfoVo.setType(basicInfo.getType());

        return trackMediaInfoVo;
    }

    @SneakyThrows
    @Override
    public void deleteMediaFile(String mediaFileId) {
        //创建Credential对象,封装api接口秘钥
        Credential cred = new Credential(vodConstantProperties.getSecretId(), vodConstantProperties.getSecretKey());
        //创建VodClient对象
        VodClient client = new VodClient(cred, "ap-guangzhou");
        //创建DeleteMediaRequest对象
        DeleteMediaRequest req = new DeleteMediaRequest();
        req.setFileId(mediaFileId);
        DeleteMediaResponse deleteMediaResponse = client.DeleteMedia(req);
        log.info(JSON.toJSONString(deleteMediaResponse));

    }
}
